<?php
/**
 * Created by PhpStorm.
 * User: dt
 * Time: 11:44
 */
namespace backend\base;
use Yii;
use backend\utils\AliyunSms\Sms;

/**
 * 服务基类
 * @package app\base
 */
class BaseService
{
    private static $_instances = array();

    /**
     * 单例初始化服务，所有服务都要集成该基类，并重写此方法
     * @param string $className
     * @return mixed
     */
    public static function getInstance($className = __CLASS__ )
    {
        if (empty(self::$_instances[$className])) {
            self::$_instances[$className] = new $className(null, null, []);
        }
        return self::$_instances[$className];
    }

    /**
     * 拼接返回信息数组
     * @param string $className
     * @return mixed
     */
    public static function getData($type,$message,$data=[])
    {
        $return = array (
            'type' => $type, //状态码
            'message' => $message, //提示信息
            'data' => $data, //提示信息
        );
        return $return;
    }

    /***
     *短信发送
     * @param $mobile $type $type $data
     * @return array
     * BaseService::sendSmsMessage('15888893748','teacher_audit_success',['name'=>'张三']);
     */
    public static function sendSmsMessage($mobile,$type='validate_code',$data=[])
    {
        $template = Yii::$app->params['sms']['template'][$type];
        $new_template = [];
        $out_id = time().mt_rand(10000,99999);
        if($data) {
            foreach ($data as $key=>$val){
                if(isset($template[$key]) && $key!= 'template_code'){
                    $new_template[$key]=$val;
                }
            }
        }
        $demo = new Sms(
            Yii::$app->params['sms']['key'],
            Yii::$app->params['sms']['secret']
        );
        $response = $demo->sendSms(
            Yii::$app->params['sms']['sign_name'], // 短信签名
            $template['template_code'], // 短信模板编号
            $mobile, // 短信接收者
            $new_template,
            $out_id
        );
        return ['code'=>$response->Code,'message'=>$response->Message,'biz_id'=>$response->BizId];
    }

    /**
     * 短信查询
     * @param $mobile
     * @param $sendDate
     * @param int $page
     * @param int $pageSize
     * @param string $bizId
     * @return array|mixed
     * var_dump(BaseService::getSmsMessage('15888893748','20170922'));exit;
     */
    public static function getSmsMessage($mobile,$sendDate,$page=1,$pageSize=10,$bizId=''){
        $demo = new Sms(
            Yii::$app->params['sms']['key'],
            Yii::$app->params['sms']['secret']
        );
        $response = $demo->queryDetails(
            $mobile,  // phoneNumbers 电话号码
            $sendDate, // sendDate 发送时间 20170922
            $pageSize, // pageSize 分页大小
            $page, // currentPage 当前页码
            $bizId // bizId 短信发送流水号，选填
        );
        if(isset($response->Message) && $response->Message == 'OK' && isset($response->SmsSendDetailDTOs->SmsSendDetailDTO)){
            return json_decode(json_encode($response->SmsSendDetailDTOs->SmsSendDetailDTO),true);
        }
        return [];
    }


}
